package stack_and_queue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author: Zhenzhou Liu
 * @create: 2025-01-11
 * @Description:
 */
public class CatDogQueue {

    private Queue<Pet> dogQueue;
    private Queue<Pet> catQueue;

    public CatDogQueue() {
        dogQueue = new LinkedList<>();
        catQueue = new LinkedList<>();
    }

    public void addPet(Pet pet) {
        if ("dog".equals(pet.getType())) {
            dogQueue.add(pet);
        } else {
            catQueue.add(pet);
        }
    }

    public Pet pollAll() {
        if (!dogQueue.isEmpty() && !catQueue.isEmpty()) {
            if (dogQueue.peek().getCount() < catQueue.peek().getCount())
                return this.pollDog();
            else
                return this.pollCat();
        } else if (!dogQueue.isEmpty()) {
            return this.pollDog();
        } else
            return this.pollCat();
    }

    public Pet pollDog() {
        return dogQueue.poll();
    }

    public Pet pollCat() {
        return catQueue.poll();
    }

    public boolean isEmpty() {
        return dogQueue.isEmpty() && catQueue.isEmpty();
    }

    public boolean isDogEmpty() {
        return dogQueue.isEmpty();
    }

    public boolean isCatEmpty() {
        return catQueue.isEmpty();
    }
}


abstract class Pet {

    public abstract String getType();

    public abstract Integer getId();

    public abstract Integer getCount();
}

class Dog extends Pet {
    private String type;
    private Integer id;
    private Integer count;

    public Dog(String type, Integer id, Integer count) {
        this.type = type;
        this.id = id;
        this.count = count;
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public Integer getId() {
        return id;
    }

    @Override
    public Integer getCount() {
        return count;
    }
}

class Cat extends Pet {
    private String type;
    private Integer id;
    private Integer count;

    public Cat(String type, Integer id, Integer count) {
        this.type = type;
        this.id = id;
        this.count = count;
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public Integer getId() {
        return id;
    }

    @Override
    public Integer getCount() {
        return count;
    }

}


class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int rows = Integer.parseInt(reader.readLine());
        CatDogQueue queue = new CatDogQueue();

        int count = 0;
        StringBuilder builder = new StringBuilder();
        while (rows-- > 0) {
            String[] strArr = reader.readLine().split(" ");
            String opt = strArr[0];
            switch (opt) {
                case "add":
                    if ("dog".equals(strArr[1])) {
                        queue.addPet(new Dog(strArr[1], Integer.parseInt(strArr[2]), count++));
                    } else {
                        queue.addPet(new Cat(strArr[1], Integer.parseInt(strArr[2]), count++));
                    }
                    break;
                case "pollAll":
                    while (!queue.isEmpty()) {
                        Pet pet = queue.pollAll();
                        if ("dog".equals(pet.getType())) {
                            builder.append(String.format("%s %d\n", pet.getType(), pet.getId()));
                        } else {
                            builder.append(String.format("%s %d\n", pet.getType(), pet.getId()));
                        }
                    }
                    break;
                case "pollDog":
                    while (!queue.isDogEmpty()) {
                        builder.append(String.format("dog %d\n", queue.pollDog().getId()));
                    }
                    break;
                case "pollCat":
                    while (!queue.isCatEmpty()) {
                        builder.append(String.format("cat %d\n", queue.pollCat().getId()));
                    }
                    break;
                case "isEmpty":
                    builder.append(queue.isEmpty() ? "yes\n" : "no\n");
                    break;
                case "isDogEmpty":
                    builder.append(queue.isDogEmpty() ? "yes\n" : "no\n");
                    break;
                case "isCatEmpty":
                    builder.append(queue.isCatEmpty() ? "yes\n" : "no\n");
                    break;
            }
        }
        System.out.println(builder);
    }
}
